package com.yange.accountbook.ui

import android.app.DatePickerDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.yange.accountbook.databinding.OrderListFragmengBinding
import com.yange.accountbook.entity.AccountDetailEntity
import com.yange.accountbook.entity.UserInfoEntity
import com.yange.accountbook.events.AccountDetailChangeEvent
import com.yange.accountbook.net.Api
import com.yange.accountbook.net.BaseResponse
import com.yange.accountbook.net.api.RetrofitClient
import com.yange.accountbook.net.observable.RecObserver
import com.yange.accountbook.ui.adapter.DetailListOneAdapter
import com.yange.accountbook.utils.StringUtils
import com.yange.accountbook.utils.UserDao
import com.yange.accountbook.utils.showToast
import com.zhuangku.app.widget.EmptyView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*


class OrderListFragment : BaseFragment<OrderListFragmengBinding>() {


    var year = 2022
    var month = 11
    var day = 1
    private lateinit var dialog: DatePickerDialog
    lateinit var oneAdapter: DetailListOneAdapter
    override fun initView() {

        binding.dataList.layoutManager = LinearLayoutManager(activity)

        initDate()
        initListener()

        oneAdapter = DetailListOneAdapter()
        var emptyView = context?.let { EmptyView(it) }
        if (emptyView != null) {
            oneAdapter.setEmptyView(emptyView)
        }
        binding.dataList.adapter = oneAdapter

        getDataList()
    }
    private fun initDate(){
        val calendar: Calendar = Calendar.getInstance()
        buildDate(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
    }

    private fun buildDate( year1:Int, month1:Int, day1: Int){

        month = month1
        year = year1
        day = day1
        if (UserDao.getUserInfo().periodStatus && day1 < UserDao.getUserInfo().periodDay){
            month = month1 -1
            if (month < 0){
                month = 11
                year = year1 -1
            }
        }
        binding.tvYear.text = year.toString() + "年"
        binding.tvMouth.text = (month+1).toString() + "月"
    }

    private fun initListener(){
        //明细变更事件
        EventBus.getDefault().register(this)

        binding.smartRefresh.setOnRefreshListener {
            getDataList()
        }
        binding.layoutDate.setOnClickListener {
            dialog = activity?.let { it1 ->
                DatePickerDialog(
                    it1,
                    { p0, year1, month1, day1 ->
                        buildDate(year1, month1, day1)
                        getDataList()
                    },
                    year,
                    month,
                    day
                )
            }!!
            dialog.show()

        }
    }

    override fun initData() {

    }


    private fun getDataList() {

        RetrofitClient.getInstance().invokePostBody(
            activity, Api.ACCOUNT_RECORD_LIST, mapOf(
                "year" to year,
                "mon" to (month+1)
            )
        ).subscribe(object :
            RecObserver<BaseResponse<AccountDetailEntity>>(activity, false, false) {
            override fun onSuccess(t: BaseResponse<AccountDetailEntity>?) {
                binding.tvOut.text = "￥" + StringUtils.getNoMoreThanTwoDigits(t?.data?.outMoney!!)
                binding.tvIn.text = "￥" + StringUtils.getNoMoreThanTwoDigits(t?.data?.inMoney!!)
                binding.smartRefresh.finishRefresh()
                binding.smartRefresh.finishLoadMore()
                oneAdapter.setNewInstance(t?.data?.days)

            }

            override fun onFail(msg: String, code: Int) {
                binding.smartRefresh.finishRefresh()
                binding.smartRefresh.finishLoadMore()
                showToast(msg)
            }
        })
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAccountDetailChange(event: AccountDetailChangeEvent) {
        getDataList()
    }
}